package puppy.graph;

import java.awt.Dimension;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.regex.Pattern;

import javax.swing.JFrame;

import puppy.query.QueryNGramExtractor;

import util.io.FileInput;
import util.io.TextFileLoader;
import util.string.URLCleaner;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;

import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import fileloaders.parsers.InLineParser;

public class CreateGraphTopics {

	private static final Integer EDGE_THRESHOLD = 3;
	private static final Integer NODE_THRESHOLD = 3;
	
	
	private static final Integer EDGE_THRESHOLD_KIDS = 2;
	private static final Integer NODE_THRESHOLD_KID = 3;
	
	
	/**
	 * @param args
	 */
	public int n_nodes = 0;
	public int n_edges = 0;

	// Generate
	// id_url, id_tag, url,tag,freq_tag,freq_url, freq_ut,type
	
	
	//http://5.ua/	tv_news	adults	11953	10	3	5.ua

	
	public UndirectedSparseGraph<DeliciousNode, DeliciousEdge> graph= null;
	
	private Pattern tab = Pattern.compile("\t");
	public Hashtable<String,String> urls_simplified = new  Hashtable<String,String> ();
	
	public QueryNGramExtractor query_phraser = new QueryNGramExtractor();
	public Hashtable<String, DeliciousNode> nodes = new Hashtable<String, DeliciousNode>();

	private int _id_url = 0;
	private int _id_tag = 1;
	private int _url = 2;
	private int _tag = 3;
	private int _freq_u = 5;
	private int _freq_tag = 4;
	private int _freq_ut = 6;
	private int _age = 7;
	
	private int _adult_freq_tag= 8;
	private int _adult_freq_url=9;
	private int _adult_freq_domain=10;
	
	private boolean domain_freq_flag=false;
	
	
	AgeAggregator agg= null;

	
	//HashSet<Integer> vertexes = new HashSet<Integer>();
//	public Hashtable<String,DeliciousNode> tag_names = new Hashtable<String,DeliciousNode>(); 
	//public Hashtable<String,Integer> urls_totals = new Hashtable<String,Integer>();
	//
	//public Hashtable<String,Integer> tags_totals = new Hashtable<String,Integer>();

		
	
	
	public CreateGraphTopics(AgeAggregator agg){
		this.agg=agg;
		
	}
	
	//kids kids-teens teens teens-mteens mteesn all adults
	
	
	public void initGraph(String path) {

		FileInput in = new FileInput(path);
		graph = new UndirectedSparseGraph<DeliciousNode, DeliciousEdge>();
		String line = in.readString();
		
		int j =0;
		while (line != null) {
		//	System.out.println("Adding " + line);
			dumpLineIntoGraphDirected(graph, line);
			line = in.readString();
			j++;
	//	if(j==5) return;
		}
	}


	public void aggreageteValueInHash(Hashtable<String,Integer>h, String key, int value){
		
		if(h.containsKey(key)){
			
			h.put(key, h.get(key)+ value);
			
		}else{
			
			h.put(key, value);
			
		}
		
	}
	
	
	
	private void dumpLineIntoGraphDirected(
			UndirectedSparseGraph<DeliciousNode, DeliciousEdge> graph,
			String line) {

		String[] fields =  tab.split(line);

		//System.out.println(line);
		line =null;
		Hashtable<String, Object[]> f = InLineParser.parseGraphEnrichedLineAgg(fields,agg);
		
		
		String[] url_tag=  (String[]) f.get("keys");

		
		f.remove("keys");
		Enumeration<String> keys = f.keys();
		
		DeliciousNode url=null;
		DeliciousNode tag=null;
	
		if(nodes.containsKey(url_tag[0])){
			url = nodes.get(url_tag[0]);
			
		}
		
		if(nodes.containsKey(url_tag[1])){
			tag= nodes.get(url_tag[1]);
			
		}
		
		DeliciousEdge edge =null;
		Float freq_ut=0f;
		
		
		
		String age= "kids";
		Float []freqs  = (Float[]) f.get(age);
		
		
		
		Float freq_tag =freqs[0]; 
		Float freq_url=freqs[1];
		freq_ut=freqs[2];
		
		Float adult_freq_tag=freqs[3]; 
		Float adult_freq_url=freqs[4];
		
		if(freq_tag==0) return;
				
		/**
		 * Verify why this is neccesary
		 */
		url=null;
		tag=null;
		
		/**
		 * end of verify why this is neccesary
		 * 
		 */
		
		/*
		 * Adding url node
		 */
		
		
		if(tag==null){
			
			tag = new DeliciousNode();
			tag.setName(url_tag[1]);
			tag.setType(false);
	    	tag.setProb_adults(adult_freq_tag);
	    	nodes.put(url_tag[1], tag);
	    	//add node to trie dictionary
			//if(name.contains("_")){
			//query_phraser.addItem(name);
			//}
			
		}
		
		
		if(url==null){
			url = new DeliciousNode();
			url.setName(url_tag[0]);
			url.setType(true);
			
			url.setProb_adults(adult_freq_url);
			
			
			
			nodes.put(url_tag[0], url);
			
			
			 String simple =URLCleaner.simplifyURL(url_tag[0],1);
				if(simple.equals("swl.nu"))System.out.println("si lo encontro: swl.nu" );
				 urls_simplified.put(simple,url_tag[0]);
		}
		url.setProb(freq_url);
			
		
		tag.setProb(freq_tag);
					
		//	aggreageteValueInHash(urls_totals,age, freq_url);	
		//create edge
		
		edge = new DeliciousEdge();
		edge.tag=tag;
		edge.url=url;
		edge.setProb(freq_ut);
		
	
	
	if (tag != null && url != null) {
		graph.addVertex(tag);
		graph.addVertex(url);
		graph.addEdge(edge, url, tag);
	}
	
	
		
		
		
		//while(keys.hasMoreElements()){
			
			//String age = keys.nextElement();
			//url, tag, freq_tag, freq_url, freq_ut, type, f_t, f_u,domain 
			
			
			//if(!agg.isAgeValid(age) ){
				
				//continue;
				
		//	}
	//	}
			
	
	}
	
	
	

/*	
	
	public void createGraph1(){
		

		*//** add nodes 
		 * 
		 * 
		 *//*
	
			DeliciousNode node = new DeliciousNode();
			
			node.setId(1);
			node.setName("A");
			
			DeliciousNode node1 = new DeliciousNode();
			node1.setId(2);
			node1.setName("B");
			
			DeliciousEdge edge =  new DeliciousEdge();
		
			graph.addEdge(edge, node, node1);
		
		
	}*/

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String prefix = "/Users/sergioduarte/projects/data/graph/";
		
		//String path = "/media/sata__/data/delicious_graph/delicious_graphML_raw_ids_dmoz.txt";
		String path = prefix+ "delicious_graphML_raw_ids_dmoz_youth_clean_kids.txt";
	
		     
	
	}

}